package com.cimp.service.iqy.interfaces;

import com.cimp.domain.iqy.*;
import com.cimp.util.fuse.HttpUtil;
import com.cimp.util.iqy.CommonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * [简要描述]: 爱奇艺通用接口
 * [详细描述]:
 *
 * @author huojinhao
 * @date 2018/7/4
 */
@Service
public class GenerateInterface {
    
    private Logger logger = Logger.getLogger(DataInterface.class);

    /**
     * 获取频道列表
     */
    public ResultBean<Channel> queryChannelList(String url) {
        Map<String, String> params = new HashMap<String, String>();
        String token = CommonUtil.generateToken();
        params.put("token", token);
        String res = HttpUtil.doGet(url, params);
        try{
            JSONObject jsonObject = JSONObject.fromObject(res);
            Map<String, Class> classMap = new HashMap<String, Class>();
            classMap.put("data",Channel.class);
            classMap.put("subs",Sub.class);
            classMap.put("tags",Tag.class);
            ResultBean<Channel> resultBean= (ResultBean<Channel>) JSONObject.
                    toBean(jsonObject, ResultBean.class, classMap);
            return resultBean;
        }catch (Exception e){
            logger.error("queryChannelList Json parse error , the response json is:" + res, e);
        }
        return null;
    }

    /**
     * 获取EPG详情-专辑或视频信息
     */
    public MetaResultBean<MetaInfo> queryMetaInfo(String url, Long id){
        Map<String, String> params = new HashMap<String, String>();
        String token = CommonUtil.generateToken();
        params.put("token", token);
        params.put("id", String.valueOf(id));
        String res = HttpUtil.doGet(url, params);
 
        MetaResultBean<MetaInfo> resultBean = null;
        try{
            JSONObject jsonObject = JSONObject.fromObject(res);
            Map<String, Class> classMap = new HashMap<String, Class>();
            classMap.put("data",MetaInfo.class);
            classMap.put("persons",Persons.class);
            classMap.put("vipInfo",VipInfo.class);
            resultBean= (MetaResultBean<MetaInfo>) JSONObject.
                    toBean(jsonObject, MetaResultBean.class, classMap);
        }catch (Exception e){
            logger.error("queryMetaInfo Json parse error , the response json is:" + res, e);
        }

        return resultBean;
    }

    /**
     * 查询视频列表
     */
    public ResultBean<Video> queryVideoList(String url, Long id, int stype, int epType, int pn, int ps){
        Map<String, String> params = new HashMap<String, String>();
        String token = CommonUtil.generateToken();
        params.put("token", token);
        params.put("id", String.valueOf(id));
        params.put("stype", String.valueOf(stype));
        params.put("epType", String.valueOf(epType));
        params.put("pn", String.valueOf(pn));
        params.put("ps", String.valueOf(ps));
        String res = HttpUtil.doGet(url, params);
        ResultBean<Video> resultBean = null;
        try{
            JSONObject jsonObject = JSONObject.fromObject(res);
            Map<String, Class> classMap = new HashMap<String, Class>();
            classMap.put("data",Video.class);
            classMap.put("persons",Persons.class);
            classMap.put("vipInfo",VipInfo.class);
            resultBean= (ResultBean<Video>) JSONObject.
                    toBean(jsonObject, ResultBean.class, classMap);
        }catch (Exception e){
            logger.error("queryVideoList Json parse error , the response json is:" + res, e);
        }
        return resultBean;
    }


    /**
     * 查询人物信息
     */
    public MetaResultBean<People> queryPeopleInfo(String url, Long id){
        Map<String, String> params = new HashMap<String, String>();
        String token = CommonUtil.generateToken();
        params.put("token", token);
        params.put("id", String.valueOf(id));
        String res = HttpUtil.doGet(url, params);
        MetaResultBean<People> resultBean = new MetaResultBean<People>();
        try{
            JSONObject jsonObject = JSONObject.fromObject(res);
            String code = jsonObject.getString("code");
            resultBean.setCode(code);
            if(jsonObject.has("data")){
                JSONObject data = jsonObject.getJSONObject("data");
                People people = new People();
                people.setEff(data.getInt("eff"));
                if(people.getEff() == 1){
                    people.setBirthday(data.getString("birthday"));
                    people.setBirthPlace(data.getString("birthPlace"));
                    people.setCover(data.getString("cover"));
                    people.setDesc(data.getString("desc"));
                    people.setHeight(data.getInt("height"));
                    people.setId(data.getLong("id"));
                    people.setName(data.getString("name"));
                    people.setOccupation(data.getString("occupation"));
                    List<Long> list = new ArrayList<Long>();
                    JSONArray jsonArray = data.getJSONArray("relVideos");
                    for(int i=0;i<jsonArray.size();i++){
                        Long l = jsonArray.getLong(i);
                        list.add(l);
                    }
                    people.setRelVideos(list);
                }
                resultBean.setData(people);
            }
            return resultBean;
        }catch (Exception e){
            logger.error("queryPeopleInfo Json parse error , the response json is:" + res, e);
        }
        return resultBean;
    }
}
